﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_chinh_sua_nha_hang : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetResInfo(); 
        }
    }
    protected void GetResInfo()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetResInfo";
            Dal.ParameterCollection = new string[1] { "@NhaHangID"};
            Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString()};
            DataRow dr = Dal.GetDataRow();
            txtResName.Text = dr["TenNhaHang"].ToString();
            GetResType();
            ddlResType.SelectedValue = dr["LoaiNhaHangID"].ToString();
            if (dr["IsActive"].ToString() == "false")
                rblActive.SelectedValue = "0";
            else rblActive.SelectedValue = "1";
            if (dr["OnHome"].ToString()== "false")
                rblOnHome.SelectedValue = "0";
            else rblOnHome.SelectedValue = "1";
            txtPhone.Text = dr["DienThoai"].ToString();
            txtEmail.Text = dr["Email"].ToString();
            txtWebsite.Text = dr["WebSite"].ToString();
            txtFB.Text = dr["FaceBook"].ToString();
            txtCapacity.Text = dr["SucChua"].ToString();
            txtDescription.Text = dr["MoTa"].ToString();
            txtUtility.Text = dr["TienIch"].ToString();
            CKEditorControl1.Text = dr["BaiGioiThieu"].ToString();
            LoadProvince();
            ddlProvince.SelectedValue = dr["TinhThanhPhoID"].ToString();
            LoadDistrict();
            ddlDistrict.SelectedValue = dr["HuyenQuanID"].ToString();
            LoadWard();
            ddlWard.SelectedValue = dr["XaPhuongID"].ToString();
            txtStreet.Text = dr["Duong"].ToString();
            txtHouseID.Text = dr["SoNha"].ToString();
        }
        catch { }
    }
    protected void GetResType()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandText = "SP_GetResType";
            Dal.CommandType = CommandType.StoredProcedure;
            DataTable db = Dal.GetDataTable();
            ddlResType.DataSource = db;
            ddlResType.DataTextField = db.Columns["MoTa"].ToString();
            ddlResType.DataValueField = db.Columns["LoaiNhaHangID"].ToString();
            ddlResType.DataBind();

        }
        catch
        {
            lblInfo.Text = "Lỗi khi load Loại Nhà Hàng";
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (fuAvatar.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fuAvatar.FileName);
                fuAvatar.SaveAs(Server.MapPath("~/Images/thumb-nhahang/" + filename));
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateResWithImage";
                Dal.ParameterCollection = new string[19] { "@MoTa", "@LoaiNhaHangID", "@TenNhaHang", "@BaiGioiThieu", "@HinhDaiDien","@DienThoai", "@Email", "@WebSite", "@FaceBook", "@TienIch", "@SucChua", "@IsActive", "@OnHome", "@NhaHangID", "@TinhThanhID", "@HuyenQuanID", "@XaPhuongID", "@Duong", "@SoNha" };
                Dal.ValueCollection = new string[19] { txtDescription.Text, ddlResType.SelectedValue, txtResName.Text, CKEditorControl1.Text,"Images/thumb-nhahang/" + fuAvatar.FileName,txtPhone.Text, txtEmail.Text, txtWebsite.Text, txtFB.Text, txtUtility.Text, txtCapacity.Text, rblActive.SelectedValue, rblOnHome.SelectedValue, Request.QueryString["NhaHangID"].ToString(), ddlProvince.SelectedValue, ddlDistrict.SelectedValue, ddlWard.SelectedValue, txtStreet.Text, txtHouseID.Text  };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                lblInfo.Text="Cập nhật thành công";
            }
            catch(Exception ex) {
                lblInfo.Text=ex.Message;
            }
        }
        else
        {
            try
            {
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateResWithNoneImage";
                Dal.ParameterCollection = new string[18] { "@MoTa", "@LoaiNhaHangID", "@TenNhaHang", "@BaiGioiThieu", "@DienThoai", "@Email", "@WebSite", "@FaceBook", "@TienIch", "@SucChua", "@IsActive", "@OnHome", "@NhaHangID", "@TinhThanhID", "@HuyenQuanID", "@XaPhuongID", "@Duong", "@SoNha" };
                Dal.ValueCollection = new string[18] { txtDescription.Text, ddlResType.SelectedValue, txtResName.Text, CKEditorControl1.Text, txtPhone.Text, txtEmail.Text, txtWebsite.Text, txtFB.Text, txtUtility.Text, txtCapacity.Text, rblActive.SelectedValue, rblOnHome.SelectedValue, Request.QueryString["NhaHangID"].ToString(), ddlProvince.SelectedValue, ddlDistrict.SelectedValue, ddlWard.SelectedValue, txtStreet.Text, txtHouseID.Text };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                lblInfo.Text = "Cập nhật thành công !";
            }
            catch(Exception ex) {
                lblInfo.Text=ex.Message;
            } 
        }
    }
    protected void LoadProvince()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllProvince";
            DataTable db = Dal.GetDataTable();
            ddlProvince.DataSource = db;
            ddlProvince.DataTextField = db.Columns["TenTinhThanhPho"].ToString();
            ddlProvince.DataValueField = db.Columns["TinhThanhPhoID"].ToString();
            ddlProvince.DataBind();
        }
        catch { }
    }
    protected void LoadDistrict()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetDistrictByProvinceID";
            Dal.ParameterCollection = new string[1] { "@TinhThanhPhoID" };
            Dal.ValueCollection = new string[1] { ddlProvince.SelectedValue };
            DataTable db = Dal.GetDataTable();
            ddlDistrict.DataSource = db;
            ddlDistrict.DataTextField = db.Columns["TenHuyenQuan"].ToString();
            ddlDistrict.DataValueField = db.Columns["HuyenQuanID"].ToString();
            ddlDistrict.DataBind();
        }
        catch { }
    }
    protected void LoadWard()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetWardByDistrictID";
            Dal.ParameterCollection = new string[1] { "@HuyenQuanID" };
            Dal.ValueCollection = new string[1] { ddlDistrict.SelectedValue };
            DataTable db = Dal.GetDataTable();
            ddlWard.DataSource = db;
            ddlWard.DataTextField = db.Columns["TenXaPhuong"].ToString();
            ddlWard.DataValueField = db.Columns["XaPhuongID"].ToString();
            ddlWard.DataBind();
        }
        catch
        { }
    }
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDistrict();
    }
    protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadWard();
    }
    

}
